<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>SSR 渲染 之前端 | 北城韩雨</title><meta name="keywords" content="Nuxt"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="Nuxt">
<meta property="og:type" content="article">
<meta property="og:title" content="SSR 渲染 之前端">
<meta property="og:url" content="http://example.com/2020/04/14/%E5%89%8D%E7%AB%AF/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="Nuxt">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530687&t=c2e3c97de6dbdc29e77593d5f4dc7fb5">
<meta property="article:published_time" content="2020-04-13T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="服务端渲染">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530687&t=c2e3c97de6dbdc29e77593d5f4dc7fb5"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2020/04/14/%E5%89%8D%E7%AB%AF/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'SSR 渲染 之前端',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530687&amp;t=c2e3c97de6dbdc29e77593d5f4dc7fb5')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">SSR 渲染 之前端</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-04-13T16:00:00.000Z" title="发表于 2020-04-14 00:00:00">2020-04-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/">项目实战</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">6.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>28分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="SSR 渲染 之前端"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="1-初始化"><a href="#1-初始化" class="headerlink" title="1.初始化"></a>1.初始化</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里我们使用nuxt创建了前端项目</span><br><span class="line"><span class="number">2.</span>命令</span><br><span class="line">npx create-nuxt-app &lt;项目名&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">✨  Generating Nuxt.js project <span class="keyword">in</span> felltest</span><br><span class="line">? Project name (felltest) <span class="comment">//名字</span></span><br><span class="line">? Project description (My doozie Nuxt.js project) <span class="comment">//不知道</span></span><br><span class="line">? Author name (北城韩语) <span class="comment">//作者名字</span></span><br><span class="line"><span class="comment">//使用什么包管理</span></span><br><span class="line">? Choose the package manager (Use arrow keys)</span><br><span class="line">&gt; Yarn</span><br><span class="line">  Npm</span><br><span class="line"><span class="comment">//使用的UI组件库</span></span><br><span class="line">? Choose UI framework (Use arrow keys)</span><br><span class="line">  </span><br><span class="line">  None</span><br><span class="line">  Ant Design Vue </span><br><span class="line">  Bootstrap Vue</span><br><span class="line">  Buefy</span><br><span class="line">  Bulma</span><br><span class="line">&gt; Element</span><br><span class="line">  Framevuerk</span><br><span class="line">  iView</span><br><span class="line">  Tachyons</span><br><span class="line">  Tailwind CSS</span><br><span class="line">  Vuesax</span><br><span class="line">  Vuetify.js</span><br><span class="line"><span class="comment">//使用的服务端 这里我们选koa</span></span><br><span class="line">? Choose custom server framework (Use arrow keys)</span><br><span class="line">  None (Recommended)</span><br><span class="line">  AdonisJs</span><br><span class="line">  Express</span><br><span class="line">  Fastify</span><br><span class="line">  Feathers</span><br><span class="line">  hapi</span><br><span class="line">&gt; Koa</span><br><span class="line">  Micro </span><br><span class="line"><span class="comment">//使用什么发请求 是否用单元测试</span></span><br><span class="line">? Choose Nuxt.js modules (Press &lt;space&gt; to select, &lt;a&gt; to toggle all, &lt;i&gt; to invert selection)</span><br><span class="line">&gt;(*) Axios</span><br><span class="line"> (*) Progressive Web App (PWA) Support</span><br><span class="line"> ( ) DotEnv </span><br><span class="line"><span class="comment">//使用的语法校验</span></span><br><span class="line">? Choose linting tools (Press &lt;space&gt; to select, &lt;a&gt; to toggle all, &lt;i&gt; to invert selection)</span><br><span class="line">&gt;(*) ESLint</span><br><span class="line"> (*) Prettier</span><br><span class="line"> ( ) Lint staged files</span><br><span class="line"> ( ) StyleLint </span><br><span class="line"><span class="comment">//使用的测试</span></span><br><span class="line">? Choose test framework (Use arrow keys)</span><br><span class="line">  None</span><br><span class="line">&gt; Jest</span><br><span class="line">  AVA </span><br><span class="line"><span class="comment">//使用服务端渲染</span></span><br><span class="line">? Choose rendering mode (Use arrow keys)</span><br><span class="line">&gt; Universal (SSR)</span><br><span class="line">  Single Page App  </span><br><span class="line"><span class="comment">//生成配置配文件</span></span><br><span class="line">? Choose development tools (Press &lt;space&gt; to select, &lt;a&gt; to toggle all, &lt;i&gt; to invert selection)</span><br><span class="line">&gt;(*) jsconfig.json (Recommended <span class="keyword">for</span> VS Code)</span><br><span class="line"> ( ) Semantic Pull Requests </span><br></pre></td></tr></table></figure>
<h1 id="2-项目结构目录"><a href="#2-项目结构目录" class="headerlink" title="2.项目结构目录"></a>2.项目结构目录</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> .nuxt 文件夹 <span class="comment">//这里存放的是我们的nuxt自带文件 其中有很多 比如router.js</span></span><br><span class="line"><span class="number">2.</span> assets 文件夹 <span class="comment">//里面主要存放前端的静态资源</span></span><br><span class="line"><span class="number">3.</span> components 文件夹 <span class="comment">//这里面主要是存放vue文件</span></span><br><span class="line"><span class="number">4.</span> layout 文件夹 <span class="comment">// 里面主要存放我们的视口文件 比如我们的page文件夹里的文件 我们想指定它的视口就需要在这里写</span></span><br><span class="line"><span class="number">5.</span> middleware 文件夹 <span class="comment">// 里面主要存放 我们的中间件</span></span><br><span class="line"><span class="number">6.</span> page 文件夹 <span class="comment">//里面主要存放我们的页面文件 </span></span><br><span class="line"><span class="number">7.</span> plugins 文件夹 <span class="comment">// 里面存放我们的插件 比如elementUI axios</span></span><br><span class="line"><span class="number">8.</span> <span class="keyword">static</span> 文件夹 <span class="comment">//里面存放我们的静态资源文件夹</span></span><br><span class="line"><span class="number">9.</span> store 文件夹 <span class="comment">//主要是存放我们的vuex文件 这里新建的的文件会自动模块化 不用我们挂在到index.js 也不用引入vuex</span></span><br><span class="line"><span class="number">10.</span> nuxt.config.js <span class="comment">//这个文件主要是我们配置的文件  比如我们要使用的axios需要在里面的plugins添加 然后还需要在modules里添加 </span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">nuxt.config.js 里面配置axios 和 代理</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  mode: <span class="string">&#x27;universal&#x27;</span>,</span><br><span class="line">  <span class="comment">//这里主要写我们的title 当然你也可以在页面配置</span></span><br><span class="line">  head: &#123;</span><br><span class="line">    title: <span class="string">&#x27;王正&#x27;</span> || <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">    meta: [</span><br><span class="line">      &#123; <span class="attr">charset</span>: <span class="string">&#x27;utf-8&#x27;</span> &#125;,</span><br><span class="line">      &#123; <span class="attr">name</span>: <span class="string">&#x27;viewport&#x27;</span>, <span class="attr">content</span>: <span class="string">&#x27;width=device-width, initial-scale=1&#x27;</span> &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        hid: <span class="string">&#x27;description&#x27;</span>,</span><br><span class="line">        name: <span class="string">&#x27;description&#x27;</span>,</span><br><span class="line">        content: process.env.npm_package_description || <span class="string">&#x27;&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    ],</span><br><span class="line">    link: [&#123; <span class="attr">rel</span>: <span class="string">&#x27;icon&#x27;</span>, <span class="attr">type</span>: <span class="string">&#x27;image/x-icon&#x27;</span>, <span class="attr">href</span>: <span class="string">&#x27;/favicon.ico&#x27;</span> &#125;]</span><br><span class="line">  &#125;,</span><br><span class="line">  loading: &#123; <span class="attr">color</span>: <span class="string">&#x27;#fff&#x27;</span> &#125;,</span><br><span class="line">  css: [<span class="string">&#x27;element-ui/lib/theme-chalk/index.css&#x27;</span>],</span><br><span class="line">  <span class="comment">//这里如上需要注册一下axios 我们的文件在plugins文件夹下 新建axios文件</span></span><br><span class="line">  plugins: [<span class="string">&#x27;@/plugins/element-ui&#x27;</span>, <span class="string">&#x27;@/plugins/axios&#x27;</span>],</span><br><span class="line">  buildModules: [</span><br><span class="line">    <span class="string">&#x27;@nuxtjs/eslint-module&#x27;</span></span><br><span class="line">  ],</span><br><span class="line">  modules: [</span><br><span class="line">    <span class="comment">//这里我们也要在modules注册一下</span></span><br><span class="line">    <span class="string">&#x27;@nuxtjs/axios&#x27;</span>,</span><br><span class="line">    <span class="comment">//配置代理</span></span><br><span class="line">    <span class="string">&#x27;@nuxtjs/proxy&#x27;</span></span><br><span class="line">  ],</span><br><span class="line">  axios: &#123;&#125;,</span><br><span class="line">  build: &#123;</span><br><span class="line">    transpile: [<span class="regexp">/^element-ui/</span>],</span><br><span class="line">    <span class="function"><span class="title">extend</span>(<span class="params">config, ctx</span>)</span> &#123;&#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">//配置代理</span></span><br><span class="line">  proxy: &#123;</span><br><span class="line">    <span class="string">&#x27;/api/&#x27;</span>: &#123;</span><br><span class="line">      target: <span class="string">&#x27;http://localhost:7001&#x27;</span>,</span><br><span class="line">      secure: <span class="literal">false</span>,</span><br><span class="line">      pathRewrite: &#123;</span><br><span class="line">        <span class="string">&#x27;^/api/&#x27;</span>: <span class="string">&#x27;&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="3-全局插件"><a href="#3-全局插件" class="headerlink" title="3.全局插件"></a>3.全局插件</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里我们用axios的响应来接和接口距离</span><br><span class="line"><span class="number">2.</span>我们需要要全局使用的方法 必须导出一个函数 然后里面写你的方法</span><br><span class="line"><span class="number">3.</span>然后我们需要在nuxt.config.js里的plugins里注册一下</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//响应来接和请求拦截距离</span></span><br><span class="line">plugins文件夹下的axios.js</span><br><span class="line"><span class="comment">//这里就是把响应拦截和请求拦截导出</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (&#123; store, redirect &#125;) =&gt; &#123;</span><br><span class="line">  server.interceptors.request.use(</span><br><span class="line">    (config) =&gt; &#123;</span><br><span class="line">      <span class="keyword">return</span> config</span><br><span class="line">    &#125;,</span><br><span class="line">    (err) =&gt; &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(err)</span><br><span class="line">    &#125;</span><br><span class="line">  )</span><br><span class="line">  server.interceptors.response.use(</span><br><span class="line">    (response) =&gt; &#123;</span><br><span class="line">    <span class="keyword">return</span> response</span><br><span class="line">    &#125;,</span><br><span class="line">    (err) =&gt; &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(err)</span><br><span class="line">    &#125;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//nuxt.config.js文件 里的plugins选项 这样全局就可以使用响应拦截和请求拦截了</span></span><br><span class="line">plugins: [<span class="string">&#x27;@/plugins/element-ui&#x27;</span>, <span class="string">&#x27;@/plugins/axios&#x27;</span>],</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//接口模块 注册全局接口</span></span><br><span class="line">plugins文件夹下的api-inject.js文件 这里我们写上专门的接口</span><br><span class="line"><span class="comment">//这里的http是我们axios文件封装之后导出的</span></span><br><span class="line"><span class="keyword">import</span> &#123; http &#125; <span class="keyword">from</span> <span class="string">&#x27;../plugins/axios&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (&#123; $axios &#125;, inject) =&gt; &#123;</span><br><span class="line">  inject(<span class="string">&#x27;login&#x27;</span>, <span class="function">(<span class="params">user</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> http.post(<span class="string">&#x27;/user/login&#x27;</span>, user)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//然后我们在nuxt.config.js配置文件里 在plugins选项 里注册一下</span></span><br><span class="line">plugins: [<span class="string">&#x27;@/plugins/element-ui&#x27;</span>, <span class="string">&#x27;@/plugins/axios&#x27;</span>, <span class="string">&#x27;@/plugins/api-inject&#x27;</span>],</span><br><span class="line"><span class="comment">//这样我们在全局只要能拿到this的地方就都可以使用到上面的接口函数 注意尽量别用箭头函数 this指向会出错</span></span><br><span class="line"><span class="comment">//我们在vuex中使用一下</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> actions = &#123;</span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">login</span>(<span class="params">&#123; state, commit &#125;, data</span>)</span> &#123;</span><br><span class="line">    <span class="comment">//注意这里不要箭头函数  </span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>, <span class="string">&#x27;vuex&#x27;</span>)</span><br><span class="line"> 	<span class="comment">//这里使用方法用this.$login</span></span><br><span class="line">    <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$login(data)</span><br><span class="line">    commit(<span class="string">&#x27;SET_TOKEN&#x27;</span>, ret.data.token)</span><br><span class="line">    <span class="keyword">return</span> ret</span><br><span class="line">  &#125;</span><br><span class="line">&#125;   </span><br></pre></td></tr></table></figure>
<h1 id="4-前端发送验证码逻辑"><a href="#4-前端发送验证码逻辑" class="headerlink" title="4.前端发送验证码逻辑"></a>4.前端发送验证码逻辑</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里用的elementUI </span><br><span class="line"><span class="number">2.</span>基本逻辑就是我们在邮箱验证码那里 弄一个发送按钮 然后点击 去出发我们后台写的接口</span><br><span class="line"><span class="number">3.</span>然后去自己的邮箱查询 然后填写在我们表单里</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html结构</span></span><br><span class="line">&lt;el-form-item prop=<span class="string">&quot;emailcode&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;email-code&quot;</span>&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;send-email-btn&quot;</span>&gt;</span><br><span class="line">      &lt;el-button type=<span class="string">&quot;primary&quot;</span> @click=<span class="string">&quot;sendCode&quot;</span>&gt;发送&lt;/el-button&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &lt;span <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;svg-container&quot;</span>&gt;</span><br><span class="line">      &lt;i <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-user&quot;</span>&gt; &lt;/i&gt;</span><br><span class="line">    &lt;/span&gt;</span><br><span class="line">    &lt;el-input</span><br><span class="line">      ref=<span class="string">&quot;emailcode&quot;</span></span><br><span class="line">      v-model=<span class="string">&quot;form.emailcode&quot;</span></span><br><span class="line">      placeholder=<span class="string">&quot;邮箱验证码&quot;</span></span><br><span class="line">      name=<span class="string">&quot;emailco&#x27;de&quot;</span></span><br><span class="line">    /&gt;</span><br><span class="line">&lt;/el-form-item&gt;</span><br><span class="line"><span class="comment">//发送逻辑</span></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="title">sendCode</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(</span><br><span class="line">    <span class="string">`/user/sendcode?email=<span class="subst">$&#123;<span class="built_in">this</span>.form.email&#125;</span>`</span></span><br><span class="line">  )</span><br><span class="line">  <span class="keyword">if</span> (ret.code === <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="built_in">this</span>.$notify(&#123;</span><br><span class="line">      title: <span class="string">&#x27;发送成功&#x27;</span>,</span><br><span class="line">      type: <span class="string">&#x27;success&#x27;</span></span><br><span class="line">    &#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="5-前端图片验证码"><a href="#5-前端图片验证码" class="headerlink" title="5.前端图片验证码"></a>5.前端图片验证码</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们的img图片的src属性去请求</span><br><span class="line"><span class="number">2.</span>每次我们点击这个图片验证码的时候 是切换图片验证码 这个时候应该带这时间戳在发送一次请求 然后在从新赋值给我们的src属性</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html</span></span><br><span class="line">&lt;el-form-item prop=<span class="string">&quot;emailcode&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;email-code&quot;</span>&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;send-email-btn&quot;</span>&gt;</span><br><span class="line">      &lt;img :src=<span class="string">&quot;code.captcha&quot;</span> @click=<span class="string">&quot;resetCaptcha&quot;</span> /&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &lt;span <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;svg-container&quot;</span>&gt;</span><br><span class="line">      &lt;i <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-user&quot;</span>&gt; &lt;/i&gt;</span><br><span class="line">    &lt;/span&gt;</span><br><span class="line">    &lt;el-input</span><br><span class="line">      ref=<span class="string">&quot;captcha&quot;</span></span><br><span class="line">      v-model=<span class="string">&quot;form.captcha&quot;</span></span><br><span class="line">      placeholder=<span class="string">&quot;验证码&quot;</span></span><br><span class="line">      name=<span class="string">&quot;captcha&quot;</span></span><br><span class="line">    /&gt;</span><br><span class="line">&lt;/el-form-item&gt;</span><br><span class="line"><span class="comment">//script</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line">  <span class="keyword">export</span> defualt&#123;</span><br><span class="line">      <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">          <span class="keyword">return</span> &#123;</span><br><span class="line">              code:&#123;</span><br><span class="line">                  captcha:<span class="string">&quot;/api/user/captcha&quot;</span></span><br><span class="line">              &#125;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      methods:&#123;</span><br><span class="line">		<span class="function"><span class="title">resetCaptcha</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">              <span class="comment">//每点击一次的时候就带着时间戳去重新发送请求 然后复制给我们图片的src属性</span></span><br><span class="line">              <span class="built_in">this</span>.code.captcha = <span class="string">&#x27;/api/user/captcha?_t=&#x27;</span> + <span class="keyword">new</span> <span class="built_in">Date</span>().getTime()</span><br><span class="line">            &#125;,</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;        </span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h1 id="6-前端重复密码逻辑"><a href="#6-前端重复密码逻辑" class="headerlink" title="6.前端重复密码逻辑"></a>6.前端重复密码逻辑</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里其实就是用了elementUI的表单验证功能</span><br><span class="line"><span class="number">2.</span>当我们在重复密码的时候 直接 写一个验证函数 如果重复密码不等于你输出的密码就输出错误</span><br><span class="line"><span class="number">3.</span>具体验证规则详见文档</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//代码 html</span></span><br><span class="line">&lt;el-form-item prop=<span class="string">&quot;password&quot;</span>&gt;</span><br><span class="line">    &lt;span <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;svg-container&quot;</span>&gt;</span><br><span class="line">      &lt;i <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-lock&quot;</span>&gt; &lt;/i&gt;</span><br><span class="line">    &lt;/span&gt;</span><br><span class="line">    &lt;el-input</span><br><span class="line">      ref=<span class="string">&quot;password&quot;</span></span><br><span class="line">      :key=<span class="string">&quot;passwordType&quot;</span></span><br><span class="line">      v-model=<span class="string">&quot;form.password&quot;</span></span><br><span class="line">      :type=<span class="string">&quot;passwordType&quot;</span></span><br><span class="line">      placeholder=<span class="string">&quot;密码&quot;</span></span><br><span class="line">      name=<span class="string">&quot;password&quot;</span></span><br><span class="line">    /&gt;</span><br><span class="line">    &lt;span <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;show-pwd&quot;</span> @click=<span class="string">&quot;showPwd&quot;</span>&gt;</span><br><span class="line">      &lt;i v-<span class="keyword">if</span>=<span class="string">&quot;passwordType == &#x27;password&#x27;&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-lock&quot;</span>&gt;&lt;/i&gt;</span><br><span class="line">      &lt;i v-<span class="keyword">else</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-key&quot;</span>&gt;&lt;/i&gt;</span><br><span class="line">    &lt;/span&gt;</span><br><span class="line">  &lt;/el-form-item&gt;</span><br><span class="line">  &lt;el-form-item prop=<span class="string">&quot;repassword&quot;</span>&gt;</span><br><span class="line">    &lt;span <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;svg-container&quot;</span>&gt;</span><br><span class="line">      &lt;i <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-lock&quot;</span>&gt; &lt;/i&gt;</span><br><span class="line">    &lt;/span&gt;</span><br><span class="line">    &lt;el-input</span><br><span class="line">      ref=<span class="string">&quot;repassword&quot;</span></span><br><span class="line">      v-model=<span class="string">&quot;form.repassword&quot;</span></span><br><span class="line">      placeholder=<span class="string">&quot;重复密码&quot;</span></span><br><span class="line">      name=<span class="string">&quot;repassword&quot;</span></span><br><span class="line">      type=<span class="string">&quot;password&quot;</span></span><br><span class="line">    /&gt;</span><br><span class="line">&lt;/el-form-item&gt;</span><br><span class="line"><span class="comment">//script</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line"> <span class="keyword">export</span> defualt &#123;</span><br><span class="line">     <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">         <span class="keyword">return</span>&#123;</span><br><span class="line">             registerRule&#123;</span><br><span class="line">             repassword: [</span><br><span class="line">                  &#123;</span><br><span class="line">                    required: <span class="literal">true</span>,</span><br><span class="line">                    trigger: <span class="string">&#x27;blur&#x27;</span>,</span><br><span class="line">                    validator: <span class="function">(<span class="params">rule, value, callback</span>) =&gt;</span> &#123;</span><br><span class="line">                      <span class="keyword">if</span> (value !== <span class="built_in">this</span>.form.password) &#123;</span><br><span class="line">                        callback(<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;两次输入不一致&#x27;</span>))</span><br><span class="line">                      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        callback()</span><br><span class="line">                      &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                  &#125;</span><br><span class="line">                ]</span><br><span class="line">         &#125;     </span><br><span class="line">         &#125;</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;         </span><br><span class="line">&lt;/script&gt;          </span><br></pre></td></tr></table></figure>
<h1 id="7-前端注册逻辑"><a href="#7-前端注册逻辑" class="headerlink" title="7.前端注册逻辑"></a>7.前端注册逻辑</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先你的东西要校验都通过了</span><br><span class="line"><span class="number">2.</span>点击注册时候发送请求 如果注册成功就跳转到登录页面</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html 以上b-model 绑定的字段我们就省略</span></span><br><span class="line">&lt;el-button</span><br><span class="line">    type=<span class="string">&quot;primary&quot;</span></span><br><span class="line">    style=<span class="string">&quot;width:100%;margin-bottom:30px&quot;</span></span><br><span class="line">	<span class="comment">//这里需要用事件修饰符绑定原生的DOM事件和去除默认行为</span></span><br><span class="line">    @click.native.prevent=<span class="string">&quot;handleRegister&quot;</span></span><br><span class="line">    &gt;注册&lt;/el-button</span><br><span class="line">&gt;</span><br><span class="line"><span class="comment">//script  </span></span><br><span class="line">&lt;script&gt;</span><br><span class="line">  methods:&#123;</span><br><span class="line">    <span class="function"><span class="title">handleRegister</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="built_in">this</span>.$refs.form.validate(<span class="keyword">async</span> (valid) =&gt; &#123;</span><br><span class="line">        <span class="comment">//如果校验通过了  </span></span><br><span class="line">        <span class="keyword">if</span> (valid) &#123;</span><br><span class="line">          <span class="keyword">const</span> obj = &#123;</span><br><span class="line">            email: <span class="built_in">this</span>.form.email,</span><br><span class="line">            password: md5(<span class="built_in">this</span>.form.password),</span><br><span class="line">            emailcode: <span class="built_in">this</span>.form.emailcode,</span><br><span class="line">            captcha: <span class="built_in">this</span>.form.captcha,</span><br><span class="line">            nickname: <span class="built_in">this</span>.form.nickname</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="comment">//我们就去发送请求</span></span><br><span class="line">          <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http.post(<span class="string">&#x27;/user/register&#x27;</span>, obj)</span><br><span class="line">          <span class="comment">//如果后端给我们返回成功 我们就提示之类的</span></span><br><span class="line">          <span class="keyword">if</span> (ret.code === <span class="number">0</span>) &#123;</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">this</span>.$notify(&#123;</span><br><span class="line">              title: <span class="string">&#x27;注册失败&#x27;</span>,</span><br><span class="line">              type: <span class="string">&#x27;warning&#x27;</span></span><br><span class="line">            &#125;)</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;,  </span><br><span class="line">  &#125;      </span><br><span class="line">&lt;/script&gt;        </span><br></pre></td></tr></table></figure>
<h1 id="8-前端登录逻辑"><a href="#8-前端登录逻辑" class="headerlink" title="8.前端登录逻辑"></a>8.前端登录逻辑</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>前端登录要把密码和邮箱传送过去 </span><br><span class="line"><span class="number">2.</span>登录的请求我们需要在vue里发送 因为返还的token我们需要存放在vuex里</span><br><span class="line"><span class="number">3.</span>前端项目目录里的store文件夹 下的js文件 就会自动挂在到我们的store实列上</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html</span></span><br><span class="line">&lt;el-button</span><br><span class="line">    type=<span class="string">&quot;primary&quot;</span></span><br><span class="line">    style=<span class="string">&quot;width:100%;margin-bottom:30px&quot;</span></span><br><span class="line">    @click.native.prevent=<span class="string">&quot;handlelogin&quot;</span></span><br><span class="line">    &gt;登录&lt;/el-button</span><br><span class="line">&gt;</span><br><span class="line"><span class="comment">//script</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line">   <span class="comment">//这里我们需要导入md5加密     </span></span><br><span class="line">   <span class="keyword">import</span> md5 <span class="keyword">from</span> <span class="string">&#x27;md5&#x27;</span>  </span><br><span class="line">   <span class="function"><span class="title">handlelogin</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">     <span class="comment">//先去走校验  </span></span><br><span class="line">      <span class="built_in">this</span>.$refs.form.validate(<span class="keyword">async</span> (valid) =&gt; &#123;</span><br><span class="line">        <span class="keyword">if</span> (valid) &#123;</span><br><span class="line">          <span class="keyword">const</span> obj = &#123;</span><br><span class="line">            email: <span class="built_in">this</span>.form.email,</span><br><span class="line">            password: md5(<span class="built_in">this</span>.form.password)</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="comment">//然后我们去出发vuex里的actions 把我们的账户和密码传送过去</span></span><br><span class="line">          <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$store.dispatch(<span class="string">&#x27;user/login&#x27;</span>, obj)</span><br><span class="line">          <span class="keyword">if</span> (ret.code === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">this</span>.$notify(&#123;</span><br><span class="line">              title: <span class="string">&#x27;登录成功&#x27;</span>,</span><br><span class="line">              type: <span class="string">&#x27;success&#x27;</span></span><br><span class="line">            &#125;)</span><br><span class="line">            <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">              <span class="built_in">this</span>.$router.push(&#123; <span class="attr">path</span>: <span class="string">&#x27;/&#x27;</span> &#125;)</span><br><span class="line">            &#125;, <span class="number">1000</span>)</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;    </span><br><span class="line"><span class="comment">//store文件夹下 user.js文件 这里我们导入自己写的axios 也可以自己写inject注入接口</span></span><br><span class="line"><span class="keyword">import</span> &#123; http &#125; <span class="keyword">from</span> <span class="string">&#x27;../plugins/axios&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> state = <span class="function">() =&gt;</span> (&#123;</span><br><span class="line">  token: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">  id: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">  email: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">  nickname: <span class="string">&#x27;&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> mutations = &#123;</span><br><span class="line">    SET_TOKEN: <span class="function">(<span class="params">state, token</span>) =&gt;</span> &#123;</span><br><span class="line">        state.token = token</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;    </span><br><span class="line"><span class="comment">//这里login.vue文件里的login方法 出发了actions</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> actions = &#123;</span><br><span class="line">  <span class="keyword">async</span> <span class="function"><span class="title">login</span>(<span class="params">&#123; state, commit &#125;, data</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> ret = <span class="keyword">await</span> http.post(<span class="string">&#x27;/user/login&#x27;</span>, data)</span><br><span class="line">    <span class="comment">//当然这里也可以使用我们自己的登录接口</span></span><br><span class="line">    <span class="comment">// const ret = await this.$login(data)</span></span><br><span class="line">    commit(<span class="string">&#x27;SET_TOKEN&#x27;</span>, ret.data.token)</span><br><span class="line">    <span class="keyword">return</span> ret</span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>
<h1 id="9-请求拦截和响应拦截"><a href="#9-请求拦截和响应拦截" class="headerlink" title="9.请求拦截和响应拦截"></a>9.请求拦截和响应拦截</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们前端页面登录按钮 然后触发vuex里的actions里login 然后actions里的login去向后端发送登录请求</span><br><span class="line"><span class="number">2.</span>然后后端会返回给我们token 这个时候我们需要先把后台返回的数据拦截一下 这里就要在axios的响应拦截里去完成 然后存在本地存储 </span><br><span class="line"><span class="number">3.</span>这个时候上面的代码走完了 才会走你actions里的逻辑</span><br><span class="line"><span class="number">4.</span>这个时候就需要在请求拦截里添加token</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="keyword">import</span> &#123; MessageBox &#125; <span class="keyword">from</span> <span class="string">&#x27;element-ui&#x27;</span></span><br><span class="line"><span class="keyword">const</span> server = axios.create(&#123;</span><br><span class="line">  timeout: <span class="number">5000</span>,</span><br><span class="line">  baseURL: <span class="string">&#x27;/api&#x27;</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">const</span> TOKEN_KEY = <span class="string">&#x27;waze&#x27;</span></span><br><span class="line"><span class="comment">// 导出请求拦截 和响应拦截 这里需要在我们的前端项目目录(nuxt.config.js)里(plugins)配置一下 </span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (&#123; store, redirect &#125;) =&gt; &#123;</span><br><span class="line">  server.interceptors.request.use(</span><br><span class="line">    (config) =&gt; &#123;</span><br><span class="line">      <span class="keyword">const</span> token = <span class="built_in">window</span>.localStorage.getItem(TOKEN_KEY)</span><br><span class="line">      <span class="keyword">if</span> (token) &#123;</span><br><span class="line">        config.headers.common.Authorization = <span class="string">&#x27;Bearer &#x27;</span> + token</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> config</span><br><span class="line">    &#125;,</span><br><span class="line">    (err) =&gt; &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(err)</span><br><span class="line">    &#125;</span><br><span class="line">  )</span><br><span class="line">  server.interceptors.response.use(</span><br><span class="line">    (response) =&gt; &#123;</span><br><span class="line">      <span class="comment">// data是后端给你返回的数据 config是后端给你返回的接口信息其中我们可以用他来来接是部分接口</span></span><br><span class="line">      <span class="comment">// 然后对数据进行处理</span></span><br><span class="line">      <span class="keyword">const</span> &#123; data, config &#125; = response</span><br><span class="line">      <span class="comment">// 这里的逻辑就是如果你请求成功 并且你请求的接口是登录的接口 那么我就存token</span></span><br><span class="line">      <span class="keyword">if</span> (data.code === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (config.url === <span class="string">&#x27;/user/login&#x27;</span>) &#123;</span><br><span class="line">          <span class="built_in">localStorage</span>.setItem(TOKEN_KEY, data.data.token)</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span> (data.code === -<span class="number">666</span>) &#123;</span><br><span class="line">        MessageBox.confirm(<span class="string">&#x27;登录过期了&#x27;</span>, <span class="string">&#x27;过期&#x27;</span>, &#123;</span><br><span class="line">          confirmButtonText: <span class="string">&#x27;登录&#x27;</span>,</span><br><span class="line">          showCancelButton: <span class="literal">false</span>,</span><br><span class="line">          type: <span class="string">&#x27;warning&#x27;</span></span><br><span class="line">        &#125;).then(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">          <span class="built_in">localStorage</span>.removeItem(TOKEN_KEY)</span><br><span class="line">          redirect(&#123; <span class="attr">path</span>: <span class="string">&#x27;/login&#x27;</span> &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="comment">// 如果状态码是-666那么说明token过期了要换取token</span></span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// console.log(data, config)</span></span><br><span class="line">      <span class="comment">// console.log(&#x27;响应拦截&#x27;, response)</span></span><br><span class="line">      <span class="keyword">return</span> data</span><br><span class="line">    &#125;,</span><br><span class="line">    (err) =&gt; &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(err)</span><br><span class="line">    &#125;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line">Vue.prototype.$http = server</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> http = server</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//配置请求拦截和响应拦截 nuxt.config.js里的plugins配置</span></span><br><span class="line">plugins: [<span class="string">&#x27;@/plugins/element-ui&#x27;</span>, <span class="string">&#x27;@/plugins/axios&#x27;</span>]</span><br></pre></td></tr></table></figure>
<h1 id="10-新建文章"><a href="#10-新建文章" class="headerlink" title="10.新建文章"></a>10.新建文章</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>把我们的文章内容发送后端 </span><br><span class="line"><span class="number">2.</span>后端接收后  我们在经过jwt中间件的时候 已经把本人的ID都已经保存起来了</span><br><span class="line"><span class="number">3.</span>然后去表里保存</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html</span></span><br><span class="line">&lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;write-btn&quot;</span>&gt;</span><br><span class="line">  &lt;el-button type=<span class="string">&quot;primary&quot;</span> @click=<span class="string">&quot;submit&quot;</span>&gt;提交&lt;/el-button&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="comment">//script</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line">   methods:&#123;</span><br><span class="line">       <span class="keyword">async</span> <span class="function"><span class="title">submit</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http.post(<span class="string">&#x27;/article/create&#x27;</span>, &#123;</span><br><span class="line">        content: <span class="built_in">this</span>.content</span><br><span class="line">      &#125;)</span><br><span class="line">      <span class="keyword">if</span> (ret.code === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">this</span>.$notify(&#123;</span><br><span class="line">          title: <span class="string">&#x27;创建成功&#x27;</span>,</span><br><span class="line">          type: <span class="string">&#x27;success&#x27;</span>,</span><br><span class="line">          message: <span class="string">`文章《<span class="subst">$&#123;ret.data.title&#125;</span>》创建成功`</span></span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">          <span class="built_in">this</span>.$router.push(&#123; <span class="attr">path</span>: <span class="string">&#x27;/article/&#x27;</span> + ret.data.id &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">   &#125;</span><br><span class="line">&lt;/script&gt;      </span><br></pre></td></tr></table></figure>
<h1 id="11-获取文章列表"><a href="#11-获取文章列表" class="headerlink" title="11.获取文章列表"></a>11.获取文章列表</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>只需要给后端发送个请求就好</span><br><span class="line"><span class="number">2.</span>然后后端就会把文章列表的数据 传送给我们</span><br><span class="line"><span class="number">3.</span>数据格式如下</span><br><span class="line">[</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//这里是文章的ID</span></span><br><span class="line">       _id: <span class="string">&quot;5e69003d0250293e90108ff6&quot;</span></span><br><span class="line">        title: <span class="string">&quot; node.js基础实战&quot;</span></span><br><span class="line">        article: <span class="string">&quot;# node.js基础实战↵墨小邪&quot;</span></span><br><span class="line">        article_html: <span class="string">&quot;&lt;h1 id=&quot;</span>nodejs基础实战<span class="string">&quot;&gt;node.js基础实战&lt;/h1&gt;↵&lt;p&gt;墨小邪&lt;/p&gt;↵&quot;</span></span><br><span class="line">        <span class="comment">//这里就是我们上面所说的 通过在设计article表的时候 author字段的ref：user 就把我们user表里的数据关联过去了</span></span><br><span class="line">        author:&#123;</span><br><span class="line">        following: []</span><br><span class="line">        likeArticle: [<span class="string">&quot;5e69003d0250293e90108ff6&quot;</span>]</span><br><span class="line">        disArticle: []</span><br><span class="line">        _id: <span class="string">&quot;5e6900220250293e90108ff5&quot;</span></span><br><span class="line">        email: <span class="string">&quot;1052101962@qq.com&quot;</span></span><br><span class="line">        nickname: <span class="string">&quot;墨晓邪&quot;</span></span><br><span class="line">        createdAt: <span class="string">&quot;2020-03-11T15:13:38.012Z&quot;</span></span><br><span class="line">        updatedAt: <span class="string">&quot;2020-03-11T15:14:09.579Z&quot;</span></span><br><span class="line">        &#125;   </span><br><span class="line">        __proto__: <span class="built_in">Object</span></span><br><span class="line">        createdAt: <span class="string">&quot;2020-03-11T15:14:05.131Z&quot;</span></span><br><span class="line">        updatedAt: <span class="string">&quot;2020-03-11T15:17:25.366Z&quot;</span></span><br><span class="line">        views: <span class="number">12</span></span><br><span class="line">        like: <span class="number">2</span></span><br><span class="line">    &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html</span></span><br><span class="line"><span class="comment">//这里我们显示的只是文章的标题 点击之后跳转到详细文章 所以这里我们需要新建一个详细文章的页面 然后在跳转的时候把我们的文章ID传进去  新建详情文章信息下面会有</span></span><br><span class="line">&lt;div v-<span class="keyword">for</span>=<span class="string">&quot;acticle in article&quot;</span> :key=<span class="string">&quot;acticle._aid&quot;</span>&gt;</span><br><span class="line">  &lt;nuxt-link :to=<span class="string">&quot;&#x27;/article/&#x27; + acticle._id&quot;</span>&gt;</span><br><span class="line">    &#123;&#123; acticle.title &#125;&#125;</span><br><span class="line">  &lt;/nuxt-link&gt;</span><br><span class="line">&lt;/div&gt;</span><br><span class="line"><span class="comment">//script</span></span><br><span class="line"><span class="comment">//这里我们用ssr 让数据在返回的时候直接就在服务器渲染 方便seo和爬虫 写法如下</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="comment">//这里比vue的任何一个生命周期都要提前 是服务端渲染</span></span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">asyncData</span>(<span class="params">&#123; app &#125;</span>)</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> ret = <span class="keyword">await</span> app.$axios.get(<span class="string">&#x27;/api/article&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      article: ret.data.data</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//跳转到文章详情页面 </span></span><br><span class="line">&lt;nuxt-link :to=<span class="string">&quot;&#x27;/article/&#x27; + acticle._id&quot;</span>&gt;</span><br><span class="line">    &#123;&#123; acticle.title &#125;&#125;</span><br><span class="line">&lt;/nuxt-link&gt;</span><br><span class="line"><span class="comment">//写法</span></span><br><span class="line">我们根据上面的路径 在page下新建一个article文件夹 然后新建_id.vue文件 这样我们的nuxt在生成路由的时候就会自动帮我们生成<span class="string">&#x27;/article/:id&#x27;</span>这种格式 然后就可以通过<span class="built_in">this</span>.$route.params._id去拿我们的文章ID了</span><br></pre></td></tr></table></figure>
<h1 id="12-文章详情页"><a href="#12-文章详情页" class="headerlink" title="12.文章详情页"></a>12.文章详情页</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>结构就是 作者的名字 然后关注 或者 已经关注 下面是文章的标题 和具体内容 文章点赞 或者 踩</span><br><span class="line"><span class="number">2.</span>我们首先需要用路由上route的ID 去数据库里查询详情文章获取内容</span><br><span class="line"><span class="number">3.</span>关注的时候 这里指的关注作者 我们需要把作者的ID传进去 这里通过上面的查询详情文章就能把作者也查询出来</span><br><span class="line"><span class="number">4.</span>当点击喜欢或者踩的时候也是把文章的ID传送给后端 </span><br></pre></td></tr></table></figure>
<h2 id="12-1文章详情页面-UserDisplay组件"><a href="#12-1文章详情页面-UserDisplay组件" class="headerlink" title="12.1文章详情页面 UserDisplay组件"></a>12.1文章详情页面 UserDisplay组件</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这里由于结构的最上部分 就是作者的头像和名称 所以我们封装一个组件 而我后端返回来的数据里 就有作者的信息包括昵称头像等</span></span><br><span class="line"><span class="comment">//components文件夹下 新建一个 UserDisplay.vue文件</span></span><br><span class="line">点击这个时候 会跳转道个人详情页 里面会有这个人的关注和粉丝 详见后面</span><br><span class="line"><span class="comment">//父组件需要把这个人的的信息传给我 我显示提成或者头像 当点击 的时候 我会跳转道个人详情页 </span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//UserDisplay 组件 html</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;user&quot;</span>&gt;</span><br><span class="line">    <span class="comment">//点击的时候 会跳转道个人详情页面  </span></span><br><span class="line">    &lt;nuxt-link :to=<span class="string">&quot;url&quot;</span>&gt;</span><br><span class="line">      <span class="comment">//显示昵称</span></span><br><span class="line">      &#123;&#123; user.nickname &#125;&#125;</span><br><span class="line">    &lt;/nuxt-link&gt;</span><br><span class="line">    &lt;slot&gt; &lt;/slot&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"><span class="comment">//scrit</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  props: [<span class="string">&#x27;user&#x27;</span>],</span><br><span class="line">  computed: &#123;</span><br><span class="line">    <span class="comment">//跳转的时候 我们需要把这个人的ID传过去 有可能你是通过文章跳转的 就要传文章作者的ID 有可能是自己ide个人中间 你就需要传自己的ID   </span></span><br><span class="line">    <span class="function"><span class="title">url</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="string">&#x27;/user/&#x27;</span> + (<span class="built_in">this</span>.user._id || <span class="built_in">this</span>.user.id)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.user, <span class="string">&#x27;----我是ID页面&#x27;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="12-2根据ID获取指定文章"><a href="#12-2根据ID获取指定文章" class="headerlink" title="12.2根据ID获取指定文章"></a>12.2根据ID获取指定文章</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//我们在跳转到文章详情页的时候 路由中会带着文章的ID过来 然后我们发送请求去数据库里查询即可</span></span><br><span class="line"><span class="comment">//html</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;kkb-container&quot;</span>&gt;</span><br><span class="line">    <span class="comment">//结构的上面 就是作者的昵称和头像  </span></span><br><span class="line">    &lt;UserDisplay :user=<span class="string">&quot;article.author&quot;</span>&gt;</span><br><span class="line">      <span class="comment">//关注逻辑详情见下面</span></span><br><span class="line">      &lt;el-button v-<span class="keyword">if</span>=<span class="string">&quot;isFollow&quot;</span> type=<span class="string">&quot;success&quot;</span> @click=<span class="string">&quot;unfollow&quot;</span></span><br><span class="line">        &gt;已关注&lt;/el-button</span><br><span class="line">      &gt;</span><br><span class="line">      &lt;el-button v-<span class="keyword">else</span> @click=<span class="string">&quot;follow&quot;</span>&gt;关注&lt;/el-button&gt;</span><br><span class="line">    &lt;/UserDisplay&gt;</span><br><span class="line">    &lt;el-divider&gt;&lt;/el-divider&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;article&quot;</span> v-html=<span class="string">&quot;article.article_html&quot;</span>&gt;&lt;/div&gt;</span><br><span class="line">    &lt;el-divider&gt;&lt;/el-divider&gt;</span><br><span class="line"></span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;el-button :type=<span class="string">&quot;likeStatus ? &#x27;success&#x27; : &#x27;info&#x27;&quot;</span> @click=<span class="string">&quot;likeAction&quot;</span>&gt;</span><br><span class="line">        &lt;i <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-thumb&quot;</span>&gt;&#123;&#123; article.like &#125;&#125;&lt;/i&gt;</span><br><span class="line">      &lt;/el-button&gt;</span><br><span class="line">      &lt;el-button&gt;</span><br><span class="line">        &lt;i <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-thumb rotate&quot;</span>&gt;&lt;/i&gt;</span><br><span class="line">        <span class="number">0</span></span><br><span class="line">      &lt;/el-button&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"><span class="comment">//script</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">	<span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.$route.params, <span class="string">&#x27;----查询ID&#x27;</span>)</span><br><span class="line">        <span class="comment">//把路由的文章ID 赋值出来    </span></span><br><span class="line">        <span class="keyword">const</span> &#123; id &#125; = <span class="built_in">this</span>.$route.params</span><br><span class="line">        <span class="built_in">this</span>.id = id</span><br><span class="line">        <span class="comment">//调用了获取文章    </span></span><br><span class="line">        <span class="built_in">this</span>.getArticle()</span><br><span class="line">      &#125;,</span><br><span class="line">    methods:&#123;</span><br><span class="line">       <span class="comment">// 获取了有ID的文章 然后渲染到页面上 返回的数据有文章的作者 和昵称 和作者账号的ID</span></span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">getArticle</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;/article/&#x27;</span> + <span class="built_in">this</span>.id)</span><br><span class="line">      <span class="comment">// 这就是带着文章的ID去查询了指定文章 然后会给我们返回该片文章的作者和他的ID</span></span><br><span class="line">      <span class="built_in">this</span>.article = ret.data</span><br><span class="line">      <span class="comment">// 获取关注状态</span></span><br><span class="line">      <span class="built_in">console</span>.log(<span class="built_in">this</span>.article, <span class="string">&#x27;-----文章&#x27;</span>)</span><br><span class="line">      <span class="comment">//关注 我们后面会将  </span></span><br><span class="line">      <span class="built_in">this</span>.checkFollowStatus()</span><br><span class="line">    &#125;,  </span><br><span class="line">    &#125;      </span><br><span class="line">&#125;           </span><br><span class="line">&lt;/script&gt;</span><br><span class="line">        </span><br></pre></td></tr></table></figure>
<h2 id="12-3关注按钮的显示逻辑"><a href="#12-3关注按钮的显示逻辑" class="headerlink" title="12.3关注按钮的显示逻辑"></a>12.3关注按钮的显示逻辑</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先后端会给我们返回一个状态的布尔值用来判断是否关注该作者</span><br><span class="line"><span class="number">2.</span>我们前端需要定义一个初始值 然后去发送请求 获取我们的自己的用户表里的following字段是否有 该片文章的ID 然后赋值给我们的初始值</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html  详情见上面</span></span><br><span class="line">&lt;UserDisplay :user=<span class="string">&quot;article.author&quot;</span>&gt;</span><br><span class="line">  <span class="comment">//我们首先根据自己的初始值去判断是关注 还是已关注  </span></span><br><span class="line">  &lt;el-button v-<span class="keyword">if</span>=<span class="string">&quot;isFollow&quot;</span> type=<span class="string">&quot;success&quot;</span> @click=<span class="string">&quot;unfollow&quot;</span></span><br><span class="line">    &gt;已关注&lt;/el-button</span><br><span class="line">  &gt;</span><br><span class="line">  &lt;el-button v-<span class="keyword">else</span> @click=<span class="string">&quot;follow&quot;</span>&gt;关注&lt;/el-button&gt;</span><br><span class="line">&lt;/UserDisplay&gt;</span><br><span class="line"><span class="comment">//script</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">	<span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            isFollow：<span class="literal">false</span>,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.$route.params, <span class="string">&#x27;----查询ID&#x27;</span>)</span><br><span class="line">        <span class="keyword">const</span> &#123; id &#125; = <span class="built_in">this</span>.$route.params</span><br><span class="line">        <span class="built_in">this</span>.id = id</span><br><span class="line">        <span class="built_in">this</span>.getArticle()</span><br><span class="line">    &#125;,    </span><br><span class="line">    methods:&#123;</span><br><span class="line">       <span class="keyword">async</span> <span class="function"><span class="title">getArticle</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">          <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;/article/&#x27;</span> + <span class="built_in">this</span>.id)</span><br><span class="line">          <span class="comment">// 这就是带着文章的ID去查询了指定文章 然后会给我们返回该片文章的作者和他的ID</span></span><br><span class="line">          <span class="built_in">this</span>.article = ret.data</span><br><span class="line">          <span class="comment">// 获取关注状态</span></span><br><span class="line">          <span class="built_in">console</span>.log(<span class="built_in">this</span>.article, <span class="string">&#x27;-----文章&#x27;</span>)</span><br><span class="line">          <span class="comment">//这里调用关注</span></span><br><span class="line">          <span class="built_in">this</span>.checkFollowStatus()</span><br><span class="line">        &#125;, </span><br><span class="line">       <span class="comment">//我们获取文章详情的时候 直接就调用这个函数 当然还有更好的写法 这里我们偷懒 </span></span><br><span class="line">	  <span class="keyword">async</span> <span class="function"><span class="title">checkFollowStatus</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="comment">// 然后我们带这改片作者的文章ID 去我们自己的表里查询又没有关注 根据后端返回的数据来显示关注按钮的状态</span></span><br><span class="line">      <span class="keyword">const</span> isFollow = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(</span><br><span class="line">        <span class="string">&#x27;/user/isfollow/&#x27;</span> + <span class="built_in">this</span>.article.author._id</span><br><span class="line">      )</span><br><span class="line">      <span class="keyword">if</span> (isFollow.code === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">this</span>.isFollow = isFollow.data.isFollow</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="12-4关注逻辑"><a href="#12-4关注逻辑" class="headerlink" title="12.4关注逻辑"></a>12.4关注逻辑</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>关注和已经关注按钮的状态 最开始是从后台返回给我们的</span><br><span class="line"><span class="number">2.</span>如果是初始状态是关注 那么我们点击的时候 按钮就要显示已关注</span><br><span class="line"><span class="number">3.</span>就是把ID传进去 然后后端接受这个ID  先把user表查出来 然后在把following字段查出来 看看 有没有你前台传进来的这个ID  然后在返回给前台</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//按钮 </span></span><br><span class="line">&lt;UserDisplay :user=<span class="string">&quot;article.author&quot;</span>&gt;</span><br><span class="line">  &lt;el-button v-<span class="keyword">if</span>=<span class="string">&quot;isFollow&quot;</span> type=<span class="string">&quot;success&quot;</span> @click=<span class="string">&quot;unfollow&quot;</span></span><br><span class="line">    &gt;已关注&lt;/el-button</span><br><span class="line">  &gt;</span><br><span class="line">  &lt;el-button v-<span class="keyword">else</span> @click=<span class="string">&quot;follow&quot;</span>&gt;关注&lt;/el-button&gt;</span><br><span class="line">&lt;/UserDisplay&gt;</span><br><span class="line"><span class="comment">//逻辑 都需要把该篇文章的作者的ID传到后台</span></span><br><span class="line"> <span class="comment">// 点击关注 这里前端请求用put 和delete去区分是关注还是取消关注</span></span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">follow</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="comment">// 把要关注的作者的ID传送过</span></span><br><span class="line">      <span class="keyword">await</span> <span class="built_in">this</span>.$http.put(<span class="string">&#x27;/user/follow/&#x27;</span> + <span class="built_in">this</span>.article.author._id)</span><br><span class="line">      <span class="comment">//显示按钮的状态</span></span><br><span class="line">      <span class="built_in">this</span>.checkFollowStatus()</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 点击取消关注</span></span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">unfollow</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">await</span> <span class="built_in">this</span>.$http.delete(<span class="string">&#x27;/user/follow/&#x27;</span> + <span class="built_in">this</span>.article.author._id)</span><br><span class="line">      <span class="built_in">this</span>.checkFollowStatus()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
<h2 id="12-5关注列表和粉丝列表逻辑"><a href="#12-5关注列表和粉丝列表逻辑" class="headerlink" title="12.5关注列表和粉丝列表逻辑"></a>12.5关注列表和粉丝列表逻辑</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>点击个人中心的时候 会跳转到个人中心 里面会显示自己的关注的列表 和自己的粉丝列表</span><br><span class="line"><span class="number">2.</span>点击文章作者的时候会跳转到文章作者的个人中心 里面会显示他关注的列表 和他粉丝的列表</span><br><span class="line"><span class="number">3.</span>无论上面哪个操作 我们需要把ID传入到后台 如果是自己 就传自己的ID 如果是从文章那跳进来的就传文章作者的ID</span><br><span class="line"><span class="number">4.</span>前面我么用UserDisplay组件封装好了点击的时候就会跳转到个人中心页面 详情见上面</span><br><span class="line"><span class="number">5.</span>所以需要新建一个页面来显示关注的列表和粉丝列表</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 新建 user文件夹 下面的_id.vue文件 因我们是从UserDisPlay跳转过来的 这个跳转的时候有可能传的是文章作者的ID也有可能是自己的ID</span></span><br><span class="line"><span class="comment">//html</span></span><br><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;kkb-container&quot;</span>&gt;</span><br><span class="line">    &lt;h1&gt;用户中心&lt;/h1&gt;</span><br><span class="line">    &lt;el-tabs v-model=<span class="string">&quot;activeName&quot;</span>&gt;</span><br><span class="line">      &lt;el-tab-pane label=<span class="string">&quot;关注&quot;</span> name=<span class="string">&quot;following&quot;</span>&gt;</span><br><span class="line">        &lt;div v-<span class="keyword">for</span>=<span class="string">&quot;f in following&quot;</span> :key=<span class="string">&quot;f._id&quot;</span>&gt;</span><br><span class="line">          &lt;UserDisplay :user=<span class="string">&quot;f&quot;</span>&gt;&lt;/UserDisplay&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">      &lt;/el-tab-pane&gt;</span><br><span class="line">      &lt;el-tab-pane label=<span class="string">&quot;粉丝&quot;</span> name=<span class="string">&quot;followers&quot;</span>&gt;</span><br><span class="line">        &lt;div v-<span class="keyword">for</span>=<span class="string">&quot;f in followers&quot;</span> :key=<span class="string">&quot;f._id&quot;</span>&gt;</span><br><span class="line">          &lt;UserDisplay :user=<span class="string">&quot;f&quot;</span>&gt;&lt;/UserDisplay&gt;</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">        &lt;div v-<span class="keyword">if</span>=<span class="string">&quot;!followers.length&quot;</span>&gt;</span><br><span class="line">          无数据</span><br><span class="line">        &lt;/div&gt;</span><br><span class="line">      &lt;/el-tab-pane&gt;</span><br><span class="line">    &lt;/el-tabs&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line"><span class="comment">//script</span></span><br><span class="line">&lt;script&gt;</span><br><span class="line"><span class="keyword">import</span> UserDisplay <span class="keyword">from</span> <span class="string">&#x27;~/components/UserDisplay&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  components: &#123;</span><br><span class="line">    UserDisplay</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">data</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> &#123;</span><br><span class="line">      activeName: <span class="string">&#x27;following&#x27;</span>,</span><br><span class="line">      userid: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">      followers: [],</span><br><span class="line">      following: []</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">    <span class="comment">//把ID搞出来 这个ID可能是你自己的 也可能是文章作者的ID  </span></span><br><span class="line">    <span class="keyword">const</span> userid = <span class="built_in">this</span>.$route.params.id</span><br><span class="line">    <span class="built_in">this</span>.userid = userid</span><br><span class="line">    <span class="comment">// 获取关注列表</span></span><br><span class="line">    <span class="built_in">this</span>.loadFollowing()</span><br><span class="line">    <span class="comment">//获取粉丝列表  </span></span><br><span class="line">    <span class="built_in">this</span>.loadFollowers()</span><br><span class="line">  &#125;,</span><br><span class="line">  methods: &#123;</span><br><span class="line">    <span class="comment">//获取关注列表  </span></span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">loadFollowing</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;/user/&#x27;</span> + <span class="built_in">this</span>.userid + <span class="string">&#x27;/following&#x27;</span>)</span><br><span class="line">      <span class="keyword">if</span> (ret.code === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">this</span>.following = ret.data</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.following, <span class="string">&#x27;-----关注&#x27;</span>)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//获取粉丝列表  </span></span><br><span class="line">    <span class="keyword">async</span> <span class="function"><span class="title">loadFollowers</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;/user/&#x27;</span> + <span class="built_in">this</span>.userid + <span class="string">&#x27;/followers&#x27;</span>)</span><br><span class="line">      <span class="keyword">if</span> (ret.code === <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">this</span>.followers = ret.data</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="built_in">this</span>.following, <span class="string">&#x27;-----粉丝&#x27;</span>)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="12-6文章点赞和取消点赞"><a href="#12-6文章点赞和取消点赞" class="headerlink" title="12.6文章点赞和取消点赞"></a>12.6文章点赞和取消点赞</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先文章点赞和取消点赞 是通过一个按钮控制的  所以要用一个状态值 去显示你的按钮是点过赞的样式 还是没点过赞的样式</span><br><span class="line"><span class="number">2.</span>然后在通过这个按钮的状态值去发送请求 你是点赞的请求还是取消点赞的请求</span><br><span class="line"><span class="number">3.</span>首先我们需要发送一个请求 用来初始化 文章点赞 和踩</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//文章点赞或者踩的初始化</span></span><br><span class="line">&lt;el-button @click=<span class="string">&quot;likeAction&quot;</span> :type=<span class="string">&quot;likeStatus?&#x27;success&#x27;:&#x27;info&#x27;&quot;</span>&gt;</span><br><span class="line">    &lt;i <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-thumb&quot;</span>&gt;&#123;&#123;article.like&#125;&#125;&lt;/i&gt;</span><br><span class="line">&lt;/el-button&gt;</span><br><span class="line"><span class="comment">//逻辑</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>&#123;</span><br><span class="line">            likeStatus: <span class="literal">false</span>,</span><br><span class="line">            dislikeStatus:<span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      <span class="keyword">let</span> token = <span class="built_in">localStorage</span>.getItem(<span class="string">&#x27;Kkb-Token&#x27;</span>)</span><br><span class="line">      <span class="keyword">if</span>(token)&#123;</span><br><span class="line">        <span class="comment">// 调用 来初始化我们的状态用来控制 点赞 或者踩按钮的样式 和状态</span></span><br><span class="line">        <span class="built_in">this</span>.getLikeStatus()</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    methods:&#123;</span><br><span class="line">      <span class="keyword">async</span> <span class="function"><span class="title">getLikeStatus</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      <span class="comment">// 获取喜欢状态</span></span><br><span class="line">      <span class="keyword">let</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http.get(<span class="string">&#x27;/user/article/&#x27;</span>+<span class="built_in">this</span>.id)</span><br><span class="line">      <span class="keyword">if</span>(ret.code==<span class="number">0</span>)&#123;</span><br><span class="line">        <span class="built_in">this</span>.likeStatus = ret.data.like</span><br><span class="line">        <span class="built_in">this</span>.dislikeStatus = ret.data.dislike</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="built_in">console</span>.log(ret)</span><br><span class="line">    &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//html 我们初始一个状态用来控制 赞按钮的样式 也用来控制他是点赞 还是取消点赞的请求</span></span><br><span class="line">&lt;el-button @click=<span class="string">&quot;likeAction&quot;</span> :type=<span class="string">&quot;likeStatus?&#x27;success&#x27;:&#x27;info&#x27;&quot;</span>&gt;</span><br><span class="line">    &lt;i <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;el-icon-thumb&quot;</span>&gt;&#123;&#123;article.like&#125;&#125;&lt;/i&gt;</span><br><span class="line">&lt;/el-button&gt;</span><br><span class="line"><span class="comment">//逻辑</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>&#123;</span><br><span class="line">            likeStatus: <span class="literal">false</span>,</span><br><span class="line">            dislikeStatus:<span class="literal">false</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    methods:&#123;</span><br><span class="line">      <span class="comment">//点赞还是取消点赞 根据后端给我们发送的初始值去判断  </span></span><br><span class="line">      <span class="keyword">async</span> <span class="function"><span class="title">likeAction</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">      <span class="comment">//这里用一个状态 控制两个请求类型 是点赞还是取消    </span></span><br><span class="line">      <span class="keyword">let</span> type = <span class="built_in">this</span>.likeStatus ? <span class="string">&#x27;delete&#x27;</span>:<span class="string">&#x27;put&#x27;</span></span><br><span class="line">      <span class="keyword">let</span> ret = <span class="keyword">await</span> <span class="built_in">this</span>.$http[type](<span class="string">&#x27;/user/likeArticle/&#x27;</span>+<span class="built_in">this</span>.id)</span><br><span class="line">      <span class="keyword">if</span>(ret.code==<span class="number">0</span>)&#123;</span><br><span class="line">        <span class="built_in">this</span>.getLikeStatus()</span><br><span class="line">        <span class="comment">//获取按钮转台  </span></span><br><span class="line">        <span class="built_in">this</span>.getArticle()</span><br><span class="line">        <span class="built_in">this</span>.$notify(&#123;</span><br><span class="line">          title:ret.message,</span><br><span class="line">          type:<span class="string">&#x27;success&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">      &#125;    </span><br><span class="line">    &#125;,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>SSR 渲染 之前端</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2020/04/14/%E5%89%8D%E7%AB%AF/">http://example.com/2020/04/14/%E5%89%8D%E7%AB%AF/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2020-04-14</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B8%B2%E6%9F%93/">服务端渲染</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/04/14/%E6%9D%83%E9%99%90%E9%80%BB%E8%BE%91/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2016-12-29%2F0604092c7129acd0f4029c6728db7fd7%2F00001.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530724&amp;t=333c5056a813a542419e5b661ebcfcd9" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">权限逻辑</div></div></a></div><div class="next-post pull-right"><a href="/2020/04/14/%E5%90%8E%E7%AB%AF/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">SSR 渲染 之后端</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/04/14/后端/" title="SSR 渲染 之后端"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530702&t=80943e12d5239f63beafd6d1c540855f" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-14</div><div class="title">SSR 渲染 之后端</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">1.初始化</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E9%A1%B9%E7%9B%AE%E7%BB%93%E6%9E%84%E7%9B%AE%E5%BD%95"><span class="toc-text">2.项目结构目录</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%85%A8%E5%B1%80%E6%8F%92%E4%BB%B6"><span class="toc-text">3.全局插件</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-%E5%89%8D%E7%AB%AF%E5%8F%91%E9%80%81%E9%AA%8C%E8%AF%81%E7%A0%81%E9%80%BB%E8%BE%91"><span class="toc-text">4.前端发送验证码逻辑</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E5%89%8D%E7%AB%AF%E5%9B%BE%E7%89%87%E9%AA%8C%E8%AF%81%E7%A0%81"><span class="toc-text">5.前端图片验证码</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E5%89%8D%E7%AB%AF%E9%87%8D%E5%A4%8D%E5%AF%86%E7%A0%81%E9%80%BB%E8%BE%91"><span class="toc-text">6.前端重复密码逻辑</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7-%E5%89%8D%E7%AB%AF%E6%B3%A8%E5%86%8C%E9%80%BB%E8%BE%91"><span class="toc-text">7.前端注册逻辑</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8-%E5%89%8D%E7%AB%AF%E7%99%BB%E5%BD%95%E9%80%BB%E8%BE%91"><span class="toc-text">8.前端登录逻辑</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9-%E8%AF%B7%E6%B1%82%E6%8B%A6%E6%88%AA%E5%92%8C%E5%93%8D%E5%BA%94%E6%8B%A6%E6%88%AA"><span class="toc-text">9.请求拦截和响应拦截</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10-%E6%96%B0%E5%BB%BA%E6%96%87%E7%AB%A0"><span class="toc-text">10.新建文章</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#11-%E8%8E%B7%E5%8F%96%E6%96%87%E7%AB%A0%E5%88%97%E8%A1%A8"><span class="toc-text">11.获取文章列表</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#12-%E6%96%87%E7%AB%A0%E8%AF%A6%E6%83%85%E9%A1%B5"><span class="toc-text">12.文章详情页</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#12-1%E6%96%87%E7%AB%A0%E8%AF%A6%E6%83%85%E9%A1%B5%E9%9D%A2-UserDisplay%E7%BB%84%E4%BB%B6"><span class="toc-text">12.1文章详情页面 UserDisplay组件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-2%E6%A0%B9%E6%8D%AEID%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E6%96%87%E7%AB%A0"><span class="toc-text">12.2根据ID获取指定文章</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-3%E5%85%B3%E6%B3%A8%E6%8C%89%E9%92%AE%E7%9A%84%E6%98%BE%E7%A4%BA%E9%80%BB%E8%BE%91"><span class="toc-text">12.3关注按钮的显示逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-4%E5%85%B3%E6%B3%A8%E9%80%BB%E8%BE%91"><span class="toc-text">12.4关注逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-5%E5%85%B3%E6%B3%A8%E5%88%97%E8%A1%A8%E5%92%8C%E7%B2%89%E4%B8%9D%E5%88%97%E8%A1%A8%E9%80%BB%E8%BE%91"><span class="toc-text">12.5关注列表和粉丝列表逻辑</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-6%E6%96%87%E7%AB%A0%E7%82%B9%E8%B5%9E%E5%92%8C%E5%8F%96%E6%B6%88%E7%82%B9%E8%B5%9E"><span class="toc-text">12.6文章点赞和取消点赞</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>